import "package:flutter/material.dart";
import "./tabBottomBarItem.dart";

class TabBottomBar extends StatefulWidget {
  double labelFontSize; //标题文字尺寸
  double selectedLabelFontSize; //选中标题文字尺寸
  Color labelColor; //文字颜色
  Color selectLabelColor; //选中文字颜色
  Color iconColor; //图标颜色
  double iconSize; //图标尺寸
  Color selectedIconColor; //选中图标颜色
  double selectedIconSize; //选中图标尺寸
  String? backgroundImageUrl; //背景图
  double backgroundImageOpacity; //背景图透明度
  Color color; //背景色
  double height; //导航栏高度
  List<TabBottomBarItem> items;
  int initIndex; //默认选中
  Function(int)? onChange; //当前选中下标改变回调
  TabBottomBar({
    super.key,
    required this.items,
    this.color = Colors.white,
    this.backgroundImageUrl,
    required this.height,
    this.initIndex = 0,
    this.labelFontSize = 12,
    this.selectedLabelFontSize = 14,
    this.labelColor = Colors.black,
    this.selectLabelColor = Colors.black,
    this.iconColor = Colors.black,
    this.iconSize = 12.0,
    this.selectedIconColor = Colors.black,
    this.selectedIconSize = 14.0,
    this.backgroundImageOpacity = 0.35,
    this.onChange,
  });
  @override
  State<StatefulWidget> createState() {
    return _TabBottomBarState();
  }
}

class _TabBottomBarState extends State<TabBottomBar> {
  List<TabBottomBarItem> children = [];
  late int currentIndex;

  ValueNotifier notifier = ValueNotifier(null); //用于通知子组件更新状态

  @override
  void initState() {
    currentIndex = widget.initIndex;
    notifier.value = currentIndex;
    super.initState();
  }

  List<TabBottomBarItem> initChildren() {
    // print('更新菜单节点');
    List<TabBottomBarItem> children = [];
    for (int i = 0; i < widget.items.length; i++) {
      TabBottomBarItem tabItem = widget.items[i];
      tabItem.iconColor = widget.iconColor;
      tabItem.iconSize = widget.iconSize;
      tabItem.selectedIconColor = widget.selectedIconColor;
      tabItem.selectedIconSize = widget.selectedIconSize;
      tabItem.labelFontSize = widget.labelFontSize;
      tabItem.labelColor = widget.labelColor;
      tabItem.selectedLabelFontSize = widget.selectedLabelFontSize;
      tabItem.selectLabelColor = widget.selectLabelColor;
      tabItem.index = i;
      tabItem.currentIndex = currentIndex;
      tabItem.notifier = notifier;
      tabItem.onTap = (int value) {
        // print('父组件接收到下标值：$value');
        if (widget.onChange != null) widget.onChange!(value);
        setState(() {
          currentIndex = value;
          notifier.value = currentIndex;
        });
      };
      children.add(tabItem);
    }
    return children;
  }

  Widget build(BuildContext context) {
    // print('父组件当前下标值：$currentIndex');
    children = initChildren();
    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        Container(
          height: widget.height,
          width: double.infinity,
          decoration: BoxDecoration(
              color: widget.color,
              image: (widget.backgroundImageUrl != null &&
                      widget.backgroundImageUrl != '')
                  ? DecorationImage(
                      fit: BoxFit.cover,
                      image: AssetImage(widget.backgroundImageUrl as String),
                      opacity: widget.backgroundImageOpacity,
                    )
                  : null),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: children,
        ),
      ],
    );
  }
}
